package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.PrjApplication;
import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.adapter.BaseRecyclerImageViewAdapter;
import eproject.mm.com.projectsupervision.adapter.DataAdapter;
import eproject.mm.com.projectsupervision.adapter.PassengerAdapter;
import eproject.mm.com.projectsupervision.adapter.ProblemInfoDetailAdapter;
import eproject.mm.com.projectsupervision.adapter.ProblemRecyclerViewAdapter;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import eproject.mm.com.projectsupervision.widget.MyListView;
import okhttp3.OkHttpClient;

public class  ProblemInfoActivity extends Activity {
    RecyclerView rv_photos;
    PassengerAdapter passengerAdapter;
    ArrayList imageUrls;
    int poss;

    String problemId;
    TextView tv_PrjName;
    TextView tv_PlanNode;
    TextView tv_intro;
    TextView tv_date;
    TextView tv_buildUnit;
    private OkHttpClient client;
    TextView tv_creator;
    TextView tv_createtime;

    String smallImages,bigImages;

    BaseRecyclerImageViewAdapter imageViewAdapter;

    MyListView dataList = null;
    ProblemInfoDetailAdapter adapter;
    LoadingDialog loadingDialog = null;

    Button btn_modify,btn_check;
    IconFontTextview iftv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_info);

        initView();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        problemId = bundle.getString("id");
        tv_PrjName.setText(bundle.getString("projectname",""));
        tv_PlanNode.setText(bundle.getString("taskname",""));
        tv_intro.setText(bundle.getString("describes",""));
        tv_date.setText(bundle.getString("limitdate",""));
        tv_buildUnit.setText(bundle.getString("buildunitname",""));
        tv_creator.setText(bundle.getString("creatorname",""));
        tv_createtime.setText(bundle.getString("createtime",""));

        smallImages = bundle.getString("newnames2","");
        bigImages = bundle.getString("newnames","");

        dataList = (MyListView) findViewById(R.id.lv_list);
        dataList.setDivider(null);

        btn_modify = findViewById(R.id.btn_modify);
        btn_check = findViewById(R.id.btn_check);

        initPhotos();

        loadList();

        String status = bundle.getString("status","");
//        if(status.equals("0") || status.equals("2")){
//            btn_modify.setVisibility(View.VISIBLE);
//            btn_check.setVisibility(View.GONE);
//        }else if(status.equals("1")){
//            btn_modify.setVisibility(View.GONE);
//            btn_check.setVisibility(View.VISIBLE);
//        }else{
//            btn_modify.setVisibility(View.GONE);
//            btn_check.setVisibility(View.GONE);
//        }
        if(status.equals("0") || status.equals("2") || status.equals("1")){
            btn_modify.setVisibility(View.GONE);
            btn_check.setVisibility(View.VISIBLE);
        }else if(status.equals("1")){
            btn_modify.setVisibility(View.GONE);
            btn_check.setVisibility(View.GONE);
        }else{
            btn_modify.setVisibility(View.GONE);
            btn_check.setVisibility(View.GONE);
        }

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    public void initView(){
        rv_photos = (RecyclerView)findViewById(R.id.rv_photos);
        tv_PrjName = (TextView)findViewById(R.id.tv_prjName);
        tv_PlanNode = (TextView)findViewById(R.id.tv_PlanNode);
        tv_intro = (TextView) findViewById(R.id.tv_intro);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_buildUnit = (TextView)findViewById(R.id.tv_buildUnit);
        tv_creator = (TextView)findViewById(R.id.tv_creator);
        tv_createtime = (TextView)findViewById(R.id.tv_createtime);
    }



    public void initPhotos(){
        List<String> smallImagesList = new ArrayList<String>();
        List<String> bigImagesList = new ArrayList<String>();

        //imageUrls = new ArrayList();
        if(smallImages!=null && !smallImages.equals("")){
            String[] smallImagesArray = smallImages.split(",");
            String[] bigImagesArray = bigImages.split(",");

            for(int i=0;i<smallImagesArray.length;i++){
                smallImagesList.add(smallImagesArray[i]);
                bigImagesList.add("http://"+ PrjApplication.getInstance().getServerIp()+":"+PrjApplication.getInstance().getServerPort() + bigImagesArray[i]);
            }

            imageViewAdapter = new BaseRecyclerImageViewAdapter(ProblemInfoActivity.this, smallImagesList, bigImagesList);
            rv_photos.setLayoutManager(new GridLayoutManager(ProblemInfoActivity.this, 4));
            rv_photos.getLayoutManager().setAutoMeasureEnabled(true);
            rv_photos.setAdapter(imageViewAdapter);
            rv_photos.setVisibility(View.VISIBLE);
        }
    }

    public void loadList(){
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String projectStr = HttpUtil.get("/getProblemDealListByQuestion?queid="+problemId);

                    handler.sendMessage(handler.obtainMessage(1, projectStr));
                } catch (Exception e) {
                    e.printStackTrace();
                    loadingDialog.close();
                }
            }
        }).start();
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String str = (String)msg.obj;
                    Gson gson=new Gson();
                    Map resultMap = gson.fromJson(str, Map.class);
                    if ((boolean) resultMap.get("success")) {
                        List rows = (List) resultMap.get("rows");

                        adapter = new ProblemInfoDetailAdapter(ProblemInfoActivity.this,rows);
                        dataList.setAdapter(adapter);
                        dataList.deferNotifyDataSetChanged();

                        loadingDialog.close();
                    }
                    break;
                case 2:
                    loadingDialog.close();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };

    public void onModify(View view){
        Intent intent = new Intent(this, ProblemDealActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("type", "0");
        bundle.putString("problemid", problemId);
        intent.putExtras(bundle);
        startActivityForResult(intent,0);
    }
    public void onCheck(View view){
        Intent intent = new Intent(this, ProblemDealActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("type", "1");
        bundle.putString("problemid", problemId);
        intent.putExtras(bundle);
        startActivityForResult(intent,0);
    }
}
